package com.example.administrator.swredeem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.administrator.swredeem.R;
import com.example.administrator.swredeem.adapter.AdapterGongdan;
import com.example.administrator.swredeem.appBase.Config;
import com.example.administrator.swredeem.beanClass.GongdanListBean;
import com.example.administrator.swredeem.beanClass.UserListBean;
import com.example.administrator.swredeem.net.HttpXutils;
import com.example.administrator.swredeem.net.MyCallback;
import com.example.administrator.swredeem.view.adapter.UserListAdapter;
import com.example.administrator.swredeem.view.recyclerview.RecyclerItemClickSupport;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GongdanListActivity extends AppCompatActivity implements RecyclerItemClickSupport.OnItemClickListener {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private AdapterGongdan adapterGongdan;
    private GongdanListBean gongdanListBean;
    private Gson gson ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gongdan_list);
        ButterKnife.bind(this);
        init();

    }

    private void init() {
        gson = new Gson();
        gongdanListBean = new GongdanListBean();
        adapterGongdan = new AdapterGongdan(gongdanListBean.getInfo());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(GongdanListActivity.this);  //布局管理器
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.hasFixedSize();
        recyclerView.setAdapter(adapterGongdan);
        RecyclerItemClickSupport.addTo(recyclerView).setOnItemClickListener(this);

        getData();
    }

    @Override
    public void onItemClicked(RecyclerView recyclerView, int position, View v) {
        String json = gson.toJson(gongdanListBean.getInfo().get(position)) ;
        Log.d("TAG", "onItemClicked: "+json);
        Intent intent = new Intent(GongdanListActivity.this,GongdanDetailsActivity.class);
        intent.putExtra("GdDetailData",json);
        startActivity(intent);
    }

    public void getData(){
        Map<String, String> map = new HashMap<>();

//        Log.d("TAG", "init: =" + TextUtils.isEmpty(huhao) + TextUtils.isEmpty(idCrad));
        HttpXutils.getData(Config.GDList, map, new MyCallback() {
            @Override
            public void onSuccess(String result) {
                Log.d("TAG", "onSuccess: "+ result);
                gongdanListBean = gson.fromJson(result, GongdanListBean.class);
                Log.d("TAG", "onSuccess: "+ gongdanListBean.getInfo().size());

                if(gongdanListBean.getInfo().size()>0){
                    adapterGongdan = new AdapterGongdan(gongdanListBean.getInfo());
                    recyclerView.setAdapter(adapterGongdan);
                    adapterGongdan.setNewData(gongdanListBean.getInfo());
                }else {
                    Toast.makeText(GongdanListActivity.this, "查无数据", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("TAG", "onError: "+ ex.getMessage());
                Toast.makeText(GongdanListActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                Toast.makeText(GongdanListActivity.this, ex.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

}
